package com.thientan.utils

import java.awt.image.BufferedImage
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.text.SimpleDateFormat
import java.util.Date;

import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
import org.krysalis.barcode4j.tools.UnitConv



public class Utility {
	/**
	 * Format money by ##,###,###
	 * @param money
	 * @return
	 */
	public static String moneyFormat(double money){
		DecimalFormatSymbols symbols = DecimalFormatSymbols.instance
		char groupSeparator = ','
		symbols.setGroupingSeparator(groupSeparator)
		new DecimalFormat("###,###", symbols).format(money)
	}
	
	public static void generateBarcode(String data, String filePath){
		//Create the barcode bean
		Code128Bean bean = new Code128Bean()
		
		final int dpi = 90
		
		//Configure the barcode generator
		bean.setModuleWidth(UnitConv.in2mm(1.4f / dpi)) //makes the narrow bar
		bean.doQuietZone(false)
		bean.setBarHeight(18)
		
		//Open output file
		File outputFile = new File(filePath)
		OutputStream out = new FileOutputStream(outputFile)
		try {
			//Set up the canvas provider for monochrome PNG output
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(
					out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0)
		
			//Generate the barcode
			bean.generateBarcode(canvas, data)
		
			//Signal end of generation
			canvas.finish()
		} finally {
			out.close()
		}
	}
	
	public static Date increaseDays(Date date, int increment = 1) {
		if(date == null){
			return null
		}
		Calendar cal = Calendar.instance
		cal.setTime(date)
		cal.add(Calendar.DATE, increment) //minus number would decrement the days
		cal.time
	}
	
	public static Date dateWithoutTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.TO_DATE_FORMAT)
		sdf.parse(sdf.format(new Date()))
	}
}
